﻿using Biomedica.Nui.ui;
using NuiDicomPlayer.Controls;
using NuiDicomPlayer.Controls.Elements;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace NuiDicomPlayer.Subviews
{
    /// <summary>
    /// Interaction logic for NewDicomFile.xaml
    /// </summary>
    public partial class NewDicomFile : SubviewBase
    {
        private delegate void OnPerformVoiceCommand(String Command);
        List<String> FileInDirectory = new List<string>();
        private double Offset;
       
        private delegate void ShowFileInfor(DirectoryInfo path);
        private delegate void SelectFile(DirectoryInfo Path);
        public NewDicomFile()
        {
            InitializeComponent();
            FileName = "";
            
        }
        public override void ShowView()
        {
            App.ActiveWindow = this;
            this.Visibility = Visibility.Visible;
            if (!System.IO.File.Exists(RootPath))
            {
                String[] Directories = System.IO.Directory.GetDirectories(RootPath+@"\Dicom");
                if (!System.IO.Directory.Exists(RootPath))  System.IO.Directory.CreateDirectory("Dicom");
                for (int i = 0; i < Directories.Length; i++)
                {
                    NuiButton btn = new NuiButton();

                    DirectoryInfo info = new DirectoryInfo(Directories[i]);
                    // btn.Content = info.Name;
                    btn.Tag = info;
                    btn.Background = new SolidColorBrush(Colors.Blue);
                    btn.EnterdBkg = Colors.Blue;
                    btn.ExitedBkg = Colors.Blue;
                    btn.Width = FileCollections.ActualWidth / 3;
                    btn.Height = FileCollections.ActualHeight - 25;
                    btn.Margin = new Thickness(15, 15, 15, 15);
                    btn.OnHandEnteredEvent += btn_OnHandEnteredEvent;
                    btn.OnHandExitedEvent += btn_OnHandExitedEvent;
                    btn.OnHandFiredEvent += btn_OnHandFiredEvent;
                    FileCollections.Children.Add(btn);
                    FileCollections.Width += btn.Width + 15;
                }
            }
           
            FileName = "";
           Biomedica.Nui.ui.UserControlTransition.DoControlTransitionAnimation(this, -1000, 0, 1, 300, null);
        }

        void btn_OnHandExitedEvent(object sender, EventArgs e)
        {
            Biomedica.Nui.ui.UserControlTransition.DoControlScaleAnimation((NuiButton)sender, 1.01, 1, 2, 100, null);
            lburl.Content = lbfileExtension.Content = lbTotalFiles.Content = lbfilesize.Content = txtFileName.Content = "";
        }
        
        private String GetDirectoryName(string p)
        {
            return "";
        }
        void btn_OnHandFiredEvent(object sender, EventArgs e)
        {
            DirectoryInfo file_path = (DirectoryInfo)((NuiButton)sender).Tag;
            txtFileName.Content = file_path.Name;
            this.Dispatcher.Invoke(new ShowFileInfor(SelectFileInDicom), file_path);
        }
        void btn_OnHandEnteredEvent(object sender, EventArgs e)
        {
            DirectoryInfo file_path = (DirectoryInfo)((NuiButton)sender).Tag;
            txtFileName.Content = file_path.Name;
            UserControlTransition.DoControlScaleAnimation((NuiButton)sender, 1.0, 1.01, 2, 300, null);
            this.Dispatcher.Invoke(new SelectFile(ShowFileInformations), file_path);
            
        }
        
        private void ShowFileInformations(DirectoryInfo path)
        {
            lburl.Content = path.FullName;
            long size = 0;
            String[] files = System.IO.Directory.GetFiles(path.FullName, @"*.dcm");
            for (int i = 0; i < files.Length; i++)
            {
                FileInfo _info = new FileInfo(files[i]);
                size += (_info.Length)/100000;
                
            }

            lbfilesize.Content = size.ToString();
            lbTotalFiles.Content = files.Length.ToString();
            FileInfo __info = new FileInfo(files[0]);
            lbfileExtension.Content = __info.Extension;
        }
        private void SelectFileInDicom(DirectoryInfo path)
        {
            this.FileName = path.FullName;
            this.WindowResult = DialogResults.OK;
            HideView();
        }
        public override void OnLocalEndTransitionEnded(object sender, EventArgs e)
        {
            base.OnLocalEndTransitionEnded(sender, e);

        }
        public string RootPath { get; set; }
        public string FileName { get; set; }
        private void btnCloseDicomFile_OnHandFiredEvent_1(object sender, EventArgs e)
        {
            this.WindowResult = DialogResults.CANCLE;
            this.HideView();
        }
        private void ZoneuserControl_OnHandFiredEvent_1(object sender, EventArgs e)
        {
            //FileCollections.ScrollOwner.ScrollToHorizontalOffset(-FileCollections.ActualWidth / 5);

            _scroll.ScrollToHorizontalOffset(Offset);
            Offset += 10;
            this.UpdateLayout();
        }

        private void ZoneuserControl_OnHandFiredEvent_2(object sender, EventArgs e)
        {
            _scroll.ScrollToHorizontalOffset(Offset);
            Offset -= 10;
            this.UpdateLayout();
        }


        #region Speech

        private void PerformVoiceCommand(string voicecommand)
        {
            switch (voicecommand)
            {
                
                case ("kinect close"):
                    this.Dispatcher.Invoke(new EventHandler(btnCloseDicomFile_OnHandFiredEvent_1), this, new EventArgs());
                    break;
                
            }
        }

        internal override void DoSpeechCommand(System.Speech.Recognition.RecognitionResult recognitionResult)
        {
            this.Dispatcher.Invoke(new OnPerformVoiceCommand(PerformVoiceCommand), recognitionResult.Text);
        }

        #endregion


    }
}
